﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Security;

namespace GameSchool.Models
{
    /// <summary>
    /// Person er gagnaklasi sem geymir þær upplýsingar sem þarf til að skrá nýjan notanda í gagnagrunninn. 
    /// </summary>  
    
    [Table("gs_person")]
    public class Person
    {
        [Key]
        [Column("person_id")]
        public int PersonId { get; set; }

        [Column("fullname")]
		[Display(Name = "Nafn")]
		public string Fullname { get; set; }

        [Column("username")]
		[Display(Name = "Notendanafn")]
        public string Username { get; set; }

        [Column("ssn")]
		[Display(Name = "Kennitala")]
        public string Ssn { get; set; }
        
        [Column("address")]
		[Display(Name = "Heimilisfang")]
        public string Address { get; set; }

        [Column("zip_code")]
		[Display(Name = "Póstnúmer")]
        public int? ZipCode { get; set; }

        [Column("phone_number")]
		[Display(Name = "Símanúmer")]
        public string PhoneNumber { get; set; }

        [Display(Name = "Stjórnandi")]
        [NotMapped]
        public bool IsAdmin { 
            get
            {
                return (this.Username != null) && Roles.IsUserInRole(this.Username, "Administrators");
            }
            set
            {
                if (this.Username == null)
                {
                    return;
                }
                if (value)
                {
                    if (!(Roles.IsUserInRole(this.Username, "Administrators")))
                    {
                        Roles.AddUserToRole(this.Username, "Administrators");
                    }
                }
                else
                {
                    if ((Roles.IsUserInRole(this.Username, "Administrators")))
                    {
                        Roles.RemoveUserFromRole(this.Username, "Administrators");
                    }
                }
            }

        }

        [Display(Name = "Kennari")]
        [NotMapped]
        public bool IsTeacher 
        {
            get
            {
                return (this.Username != null) &&  Roles.IsUserInRole(this.Username, "Teachers");
            }
            set
            {
                if (this.Username == null)
                {
                    return;
                }
                if (value)
                {
                    if (!(Roles.IsUserInRole(this.Username, "Teachers")))
                    {
                        Roles.AddUserToRole(this.Username, "Teachers");
                    }
                }
                else
                {
                    if ((Roles.IsUserInRole(this.Username, "Teachers")))
                    {
                        Roles.RemoveUserFromRole(this.Username, "Teachers");
                    }
                }


            }

        }
        [Display(Name = "Nemandi")]
        [NotMapped]
        public bool IsStudent 
        {

            get
            {
                return (this.Username != null) && Roles.IsUserInRole(this.Username, "Students");
            }
            set
            {
                if (this.Username == null)
                {
                    return;
                }
                if (value)
                {
                    if (!(Roles.IsUserInRole(this.Username, "Students")))
                    {
                        Roles.AddUserToRole(this.Username, "Students");
                    }
                }
                else
                {
                    if ((Roles.IsUserInRole(this.Username, "Students")))
                    {
                        Roles.RemoveUserFromRole(this.Username, "Students");
                    }
                }

            }

        }
    }
}